package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.no.to.SkuReductionTo;
import com.atguigu.common.no.utils.R;
import com.atguigu.gulimall.product.dao.SpuInfoDescDao;
import com.atguigu.gulimall.product.entity.*;
import com.atguigu.gulimall.product.feign.CouponFeignService;
import com.atguigu.gulimall.product.service.*;
import com.atguigu.gulimall.product.vo.*;
import javafx.geometry.Bounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.no.utils.PageUtils;
import com.atguigu.common.no.utils.Query;

import com.atguigu.gulimall.product.dao.SpuInfoDao;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.w3c.dom.Attr;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Autowired
    private SpuInfoDescDao spuInfoDescDao;

    @Autowired
    private SpuImagesService spuImagesService;
    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;
@Autowired
    private SkuInfoService skuInfoService;
@Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;


    private CouponFeignService couponFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveSpuInfo(SpuSaveVo vo) {
        //1保存spu基本信息 pms_spu_info
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(vo, spuInfoEntity);
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        this.savemaperspuinfo(spuInfoEntity);


        //2保存spu 的描述图片 spu_info_desc
        SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
        List<String> desc = vo.getDecript();
        spuInfoDescEntity.setSpuId(spuInfoEntity.getId());
        System.out.println(spuInfoEntity);
        System.out.println(spuInfoDescEntity.getSpuId() + "spuid");
        spuInfoDescEntity.setDecript(String.join(",", desc));
        spuInfoDescService.savespuinfordesc(spuInfoDescEntity);


        //3保存spu图片集 spu_image
        List<String> images = vo.getImages();
        spuImagesService.saveimage(spuInfoEntity.getId(), images);


        //4保存spu规格参数 product_attr_value
        List<BaseAttrs> baseAttrsattrs = vo.getBaseAttrs();
        List<ProductAttrValueEntity> productAttrValueEntities = baseAttrsattrs.stream().map(attrs -> {
            ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
            productAttrValueEntity.setId(attrs.getAttrId());
            AttrEntity attrEntity = attrService.getById(attrs.getAttrId());
            productAttrValueEntity.setAttrName(attrEntity.getAttrName());
            productAttrValueEntity.setAttrValue(attrs.getAttrValues());
            productAttrValueEntity.setQuickShow(attrs.getShowDesc());
            productAttrValueEntity.setSpuId(spuInfoEntity.getId());
            return productAttrValueEntity;
        }).collect(Collectors.toList());
        productAttrValueService.saveproductentitylit(productAttrValueEntities);

        //5保存当前spu信息
        //5.0spu积分信息



        Bounds bounds = vo.getBounds();

        SpuBoundTo spuBoundTo = new SpuBoundTo();

        BeanUtils.copyProperties(bounds, spuBoundTo);

        spuBoundTo.setSpuId(spuInfoEntity.getId());

        R r = couponFeignService.saveSpuBounds(spuBoundTo);

        if (r.getCode() != 0) {
            log.error("远程保存积分信息失败");
        }
        //5.1.sku基本信息 sku_info
        List<Skus> skuses = vo.getSkus();
        if (skuses != null && skuses.size()>0) {

            skuses.forEach(item -> {

                String defaultimg = "";

                for (Images image : item.getImages()) {
                    System.out.println(image.getDefaultImg()+"图片遍历");
                    if (image.getDefaultImg() == 1) {

                        defaultimg = image.getImgUrl();
                        System.out.println(defaultimg+"默认图片");
                    }
                }
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                BeanUtils.copyProperties(item, skuInfoEntity);




                skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());

                skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());

                skuInfoEntity.setSaleCount(0l);

                skuInfoEntity.setSpuId(spuInfoEntity.getId());

                skuInfoEntity.setSkuDefaultImg(defaultimg);
                System.out.println(skuInfoEntity.getSkuDefaultImg()+"默认图片sku");

                skuInfoService.saveskuinfo(skuInfoEntity);


                Long skuid = skuInfoEntity.getSkuId();


                List<SkuImagesEntity> skuImagesEntity1 = item.getImages().stream().map(images1 -> {

                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    skuImagesEntity.setSkuId(skuid);
                    skuImagesEntity.setImgUrl(images1.getImgUrl());
                    skuImagesEntity.setDefaultImg(images1.getDefaultImg());

                    return skuImagesEntity;
                }).filter(entity->
                        {
                            return !StringUtils.isEmpty( entity.getImgUrl());
                        }


                ).collect(Collectors.toList());



                //5.2 图片信息 sku_image
                skuImagesService.saveBatch(skuImagesEntity1);
                List<Attr> attrs = item.getAttr();
                List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = attrs.stream().map(attr -> {
                    SkuSaleAttrValueEntity attrValueEntity = new SkuSaleAttrValueEntity();
                    BeanUtils.copyProperties(attr, attrValueEntity);
                    attrValueEntity.setSkuId(skuid);
                    return attrValueEntity;
                }).collect(Collectors.toList());


                //5.3 sku销售属性 saale-attr_value
                skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);

                //55.4 sku优惠信息、满减
                SkuReductionTo skuReductionTo = new SkuReductionTo();
                BeanUtils.copyProperties(item, skuReductionTo);
                if (skuReductionTo.getFullCount() > 0 || skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
                    R r1 = couponFeignService.saveSkuReduction(skuReductionTo);
                    if (r1.getCode() != 0) {
                        log.error("远程保存sku优惠信息失败");
                    }


                }

            });
        }









    }



    @Override
    public void savemaperspuinfo(SpuInfoEntity spuInfoEntity) {
        this.baseMapper.insert(spuInfoEntity);
    }

    @Override
    public void savemapspuinfodesc(SpuInfoDescEntity spuInfoDescEntity) {
        spuInfoDescDao.insert(spuInfoDescEntity);
    }
}